//
//  BuildDetailTableViewController.m
//  Tospur-text
//
//  Created by Tospur_hewei on 15/9/25.
//  Copyright (c) 2015年 Tospur_hewei. All rights reserved.
//

#import "BuildDetailTableViewController.h"
#import "GroundGlass.h"
#import "BuildListDetailViewController.h"
#import "BuildDetailTableViewCell.h"
#import "SearchModel.h"
#import <UIImageView+WebCache.h>
#import <AFNetworking.h>
#include "Encryption.h"
@interface BuildDetailTableViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,) BOOL Click;
@property (nonatomic,strong) UIView *selectView;
@property (nonatomic,strong) UIButton *button1;
@property (nonatomic,strong) UIButton *button2;
@property (nonatomic,strong) UIButton *button3;
@end

@implementation BuildDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self postRequest:@""];
    self.navigationItem.title = @"楼盘列表";
    

    self.tableView.frame = CGRectMake(0, 64, 375, 667);
    GroundGlass *backImage = [[GroundGlass alloc]initWithFrame:[UIScreen mainScreen].bounds imageName:@"楼.jpg" style:UIBlurEffectStyleExtraLight];
    self.tableView.backgroundView = backImage;
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    
    self.selectView = [[UIView alloc]init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    _selectView.backgroundColor = [UIColor colorWithRed:0.383 green:0.753 blue:1.000 alpha:1.000];
    [self.view addSubview:_selectView];
    
//    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
//    button1.frame = CGRectMake(50, 50, 50, 30);
//    [button1 setTitle:@"价格" forState:UIControlStateNormal];
//    [button1 addTarget:self action:@selector(buttonsAction:) forControlEvents:UIControlEventTouchUpInside];
//    button1.backgroundColor = [UIColor cyanColor];
//    [self.selectView addSubview:button1];
    
    [self.tableView registerClass:[BuildDetailTableViewCell class] forCellReuseIdentifier:@"buildCell"];
    self.Click = YES;
}



- (void)rightButtonAction
{
    if (self.Click == YES) {
        self.selectView.frame = CGRectMake(0, 0, 375, 100);
        [self.view bringSubviewToFront:self.selectView];
        
        self.button1 = [UIButton buttonWithType:UIButtonTypeSystem];
        _button1.frame = CGRectMake(50 , 30, 50, 30);
        _button1.tag = 100;
        [_button1 setTitle:@"价格" forState:UIControlStateNormal];
        [_button1 addTarget:self action:@selector(buttonsAction:) forControlEvents:UIControlEventTouchUpInside];
        _button1.backgroundColor = [UIColor cyanColor];
        [self.selectView addSubview:_button1];
        
        self.button2 = [UIButton buttonWithType:UIButtonTypeSystem];
        _button2.frame = CGRectMake(150 , 30, 50, 30);
        _button2.tag = 101;
        [_button2 setTitle:@"距离" forState:UIControlStateNormal];
        [_button2 addTarget:self action:@selector(buttonsAction:) forControlEvents:UIControlEventTouchUpInside];
        _button2.backgroundColor = [UIColor cyanColor];
        [self.selectView addSubview:_button2];
        
        self.button3 = [UIButton buttonWithType:UIButtonTypeSystem];
        _button3.frame = CGRectMake(250 , 30, 50, 30);
        _button3.tag = 102;
        [_button3 setTitle:@"总价" forState:UIControlStateNormal];
        [_button3 addTarget:self action:@selector(buttonsAction:) forControlEvents:UIControlEventTouchUpInside];
        _button3.backgroundColor = [UIColor cyanColor];
        [self.selectView addSubview:_button3];
    }
    else
    {
        [self.button1 removeFromSuperview];
        [self.button2 removeFromSuperview];
        [self.button3 removeFromSuperview];
        self.selectView.frame = CGRectZero;
    }
    self.Click = !_Click;
}

- (void)buttonsAction:(UIButton *)button
{
    switch (button.tag) {
        case 100:
            [self postRequest:@"AvgPr"];
            break;
        case 101:
            [self postRequest:@"distance"];
            break;
        case 102:
            [self postRequest:@"ExpAmount"];
            break;
        default:
            break;
    }
}

- (void)postRequest:(NSString *)orderbyColumn
{
    AFHTTPRequestOperationManager *tx = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    tx.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    tx.requestSerializer = [AFJSONRequestSerializer serializer];
    // [tx.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
    NSString *urlStr = @"http://172.20.5.142:7001/api/GetGardenList";
    NSString *signature = [[Encryption shareInstance]sha1];
   // NSLog(@"+++++ %@",signature);
    NSString *str = [@"上海" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dict = @{
                           @"appkey": @"tospur123",
                           @"signature": signature,
                           @"OrderbyColumn": orderbyColumn,//排序方式
                           @"GardenProv": str,//所在城市,这边用固定的城市"上海"
                           @"PageSize": @"10",
                           };
    [tx POST:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",responseObject);
        NSArray *arr = [responseObject objectForKey:@"GardenList"];
        self.modelArray = [NSMutableArray array];
        for (NSMutableDictionary *dic in arr) {
            self.model = [[SearchModel alloc]init];
            _model.GardenName = dic[@"GardenName"];
            _model.GardenPic = dic[@"GardenPic"];
            _model.GardenId = dic[@"GardenId"];
            [self.modelArray addObject:_model];
          //  NSLog( @"数组有值吗=== %@",_modelArray);
        }
        [self.tableView reloadData];
        //[_HUDS hide:YES afterDelay:1];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BuildDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"buildCell" forIndexPath:indexPath];

    SearchModel *model = _modelArray[indexPath.row];
    
    cell.GardenName.text = model.GardenName;
    
    NSURL *url = [NSURL URLWithString:model.GardenPic];
    NSLog(@"aaaa ===== %@",model.GardenName);
    [cell.GardenPic sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"楼.jpg"]];
//    cell.backgroundColor = [UIColor clearColor];
    //[cell setValueForModel:model];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BuildListDetailViewController *buildVC = [[BuildListDetailViewController alloc]init];
    buildVC.searchModel = [self.modelArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:buildVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
